package org.example.browser;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;

import java.io.File;
import java.io.IOException;

import static java.lang.Thread.sleep;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-06-02
 * Time:21:08
 * Text：浏览器界面大小
 */
public class ChangeView {
    public static void Test1() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Chrome\\chrome.exe");
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");
        WebElement element = driver.findElement(By.xpath("//*[@id=\"kw\"]"));
        element.sendKeys("我爱Java");
        driver.findElement(By.cssSelector("#su")).click();

        driver.manage().window().maximize();//小屏
        sleep(3000);
        driver.manage().window().fullscreen();//全屏
        sleep(3000);
        driver.manage().window().setSize(new Dimension(300,300));
    }
    public static void Test2() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Chrome\\chrome.exe");
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");
        WebElement element = driver.findElement(By.xpath("//*[@id=\"kw\"]"));
        element.sendKeys("我爱Java");
        driver.findElement(By.cssSelector("#su")).click();

        //多层框架当中，内层框架的id定位，并且选择其中元素
        driver.switchTo().frame("f1");
        driver.findElement(By.cssSelector("#su")).click();

    }
    public static void Test3() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Chrome\\chrome.exe");
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");
        //下拉框操作
        Select select = new Select(driver.findElement(By.cssSelector("#id")));
        select.selectByIndex(2);
        select.selectByValue("123");

    }
    public static void Test4() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Chrome\\chrome.exe");
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");
        //警告框取消操作
        //driver.switchTo().alert().dismiss();
        //警告框输入
        driver.switchTo().alert().sendKeys("啥天气");
        //警告框确认
        driver.switchTo().alert().accept();
    }

    //截屏
    public static void Test5() throws InterruptedException, IOException {
        ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Chrome\\chrome.exe");
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");

        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("路径"));
    }
    public static void main(String[] args) throws InterruptedException {
        Test4();
    }
}
